<template>
  <div class="login-page">
    <img src="../assets/images/logo.png" alt="logo" />
    <main>
      <h1 class="title">{{ $store.state.title }}</h1>
      <p class="edition">版本1.0</p>
      <div class="tab-container">
        <div class="tab">
          <div
            :class="['tab-item', `tab-item_${active}`, active === 'ordinary' ? 'active' : '']"
            @click="handleChange('ordinary')">
            普通账号
          </div>
          <div
            :class="['tab-item', '`tab-item_${active}`', active === 'root' ? 'active' : '']"
            @click="handleChange('root')">
            管理账号
          </div>
          <span class="active-bar"></span>
        </div>
        <div v-show="active === 'ordinary'" class="form-container">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ordinaryForm"
            class="rule-form">
            <el-form-item label="" prop="user">
              <el-input
                placeholder="用户名"
                v-model.trim="ruleForm.user"
                clearable>
                <i class="iconfont iconyonghuming" slot="prefix"></i>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                type="password"
                placeholder="密码"
                clearable
                v-model="ruleForm.password"
                :show-password="true"
                autocomplete="new-password"
                @keydown.native.enter="submitForm">
                <i class="iconfont iconmima" slot="prefix"></i>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <div v-show="active === 'root'" class="form-container">
          <el-form
            :model="rootForm"
            :rules="rules"
            ref="rootForm"
            class="rule-form">
            <el-form-item label="" prop="user">
              <el-input
                placeholder="用户名"
                v-model.trim="rootForm.user"
                clearable>
                <i class="iconfont iconyonghuming" slot="prefix"></i>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                type="password"
                placeholder="密码"
                clearable
                v-model="rootForm.password"
                :show-password="true"
                autocomplete="new-password"
                @keydown.native.enter="submitForm">
                <i class="iconfont iconmima" slot="prefix"></i>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="btn-content">
        <el-button
          type="primary"
          :loading="loading"
          @keydown.enter="submitForm"
          @click="submitForm">
          登 录
        </el-button>
        <div class="tip">*如您首次使用该系统，查看视频监控请下载并安装视频插件</div>
      </div>
    </main>
    <p class="footer">版权所有 ©2020 浙江华东工程建设管理有限公司</p>
  </div>
</template>

<script>
import Model from '@/model';

export default {
  name: 'login',
  data() {
    return {
      active: 'ordinary',
      loading: false,
      // 普通账号信息
      ruleForm: {
        user: '',
        password: ''
      },
      // 管理账号信息
      rootForm: {
        user: '',
        password: ''
      },
      rules: {
        user: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入账号'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入密码'
          }
        ]
      }
    };
  },
  computed: {
    userInfo() {
      return {};
    }
  },
  methods: {
    /**
     * 切换账号
     * @paran type {String} 账号类型
     */
    handleChange(type) {
      this.active = type;
    },
    /**
     * 登录
     */
    submitForm() {
      let user = this.rootForm.user;
      let password = this.rootForm.password;
      let refType = 'rootForm';
      if (this.active === 'ordinary') {
        user = this.ruleForm.user;
        password = this.ruleForm.password;
        refType = 'ordinaryForm';
      }
      this.$refs[refType].validate(async valid => {
        if (valid) {
          try {
            this.loading = true;
            const { success, data: { data }} = await Model.login({
              user_id: user,
              password
            });
            this.loading = false;
            if (success) {
              if (this.$checkType(data) === 'object') {
                const user_inf = data.user || {};
                this.$setLocalStorage('x-access-token', data['x-access-token']);
                try {
                  const userInfo = JSON.stringify(user_inf);
                  this.$setLocalStorage('user_info', userInfo);
                } catch (e) {}
                // 更新用户信息和登录状态
                this.$store.commit('updateUserInfo', user_inf);
                this.$store.commit('updateLoginStatus', true);
                this.$router.push('/');
              }
            }
          } catch (e) {}
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
@import '../assets/styles/main';

.login-page {
  width: 100%;
  height: 100%;
  position: relative;
  background: url("../assets/images/logo_bg.png") no-repeat;
  background-size: cover;
  color: #fff;
  letter-spacing: 1.5px;
  > img {
    position: absolute;
    left: 50px;
    top: 50px;
    width: 360px;
    height: 120px;
  }
  .footer {
    position: absolute;
    left: 0;
    bottom: 50px;
    padding: 10px 70px;
    letter-spacing: 1px;
    background: linear-gradient(to right, #0f33a2, rgba(15, 51, 162, .6) 50%, rgba(15, 51, 162, .3) 70%, #0a1347);
  }
  main {
    width: 550px;
    height: 100%;
    position: absolute;
    right: 0;
    padding: 70px 0 0;
    .title {
      font-size: 56px;
      letter-spacing: 2px;
    }
    .edition {
      margin: 5px 0;
      color: #6584fe;
    }
    .tab-container {
      margin: 70px 0;
      .tab {
        width: 100%;
        height: 80px;
        color: #87889c;
        overflow: hidden;
        position: relative;
        .tab-item {
          height: 30px;
          line-height: 30px;
          font-size: 14px;
          float: left;
          margin-right: 20px;
          cursor: pointer;
          position: relative;
          &.active {
            transition: all .3s cubic-bezier(.645,.045,.355,1);
            font-size: 18px;
            color: #fff;
          }
          &_ordinary ~ .active-bar {
            transform: translateX(0);
          }
          &_root ~ .active-bar {
            transform: translateX(83px);
          }
        }
        .active-bar {
          position: absolute;
          bottom: 35px;
          left: 0;
          height: 10px;
          width: 50px;
          z-index: 1;
          transition: transform .3s cubic-bezier(.645,.045,.355,1);
          list-style: none;
          padding-right: 15px;
          border-top-right-radius: 2px;
          background: $linearG;
          &:after {
            content: '';
            position: absolute;
            right: -10px;
            top: 0;
            border-style: solid;
            border-width: 5px;
            border-color: transparent transparent $mainBtnBg $mainBtnBg;
          }
        }
      }
      .form-container {
        width: 400px;
        margin-top: 30px;
        .el-input {
          position: relative;
          &:before {
            content: '';
            position: absolute;
            display: block;
            height: 1px;
            width: 100%;
            background: linear-gradient(to right, transparent 0%, #6d719d 15%, transparent 40%);
          }
          &:after {
            content: '';
            position: absolute;
            display: block;
            height: 1px;
            width: 100%;
            background: linear-gradient(to left, transparent 0%, #6d719d 15%, transparent 40%);
          }
          /deep/ input {
            background-color: #232448;
            border-color: #232448;
            color: #fff;
            &::placeholder {
              color: #6584fe;
            }
            &:hover {
              border-color: #232448;
            }
          }
        }
        .is-error .el-input {
          &:before, &:after {
            display: none;
          }
        }
        /deep/ .el-input__prefix {
          line-height: 44px;
          .iconfont {
            color: #6584fe;
            padding-right: 12px;
            border-right: 1px solid rgba(101, 132, 254, 0.4);
          }
        }
        /deep/ .el-input--suffix {
          .el-input__inner {
            padding-left: 50px;
          }
        }
      }
    }
    .btn-content {
      button {
        width: 400px;
        height: 50px;
        background: $linearG;
        border-radius: 7px;
        border: none;
        font-size: 16px;
        &:hover {
          background: linear-gradient(to right, rgba(97, 129, 252, .9), rgba(31, 70, 220, .9));
        }
      }
      .tip {
        font-size: 12px;
        margin-top: 20px;
        color: #fcfcfc;
      }
    }
  }
}
</style>
